package cm.iccsoft.android.telecomcost.ui;

import java.util.ArrayList;

import cm.iccsoft.android.telecomcost.adapter.TimePeriodAdapter;
import cm.iccsoft.android.telecomcost.entities.TimePeriod;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;

public class TimePeriodSpinner extends Spinner {
	
	private ArrayList<TimePeriod> periode_list;
	private Context _context;
	private SpinnerAdapter _adapter;
	
	private void setList() throws Exception
	{
		if(_context != null)
		{
			periode_list = new ArrayList<TimePeriod>();
			periode_list.add(TimePeriod.DAY);
			periode_list.add(TimePeriod.WEEK);
			periode_list.add(TimePeriod.MONTH);
			
			setPrompt("Select a period");
			_adapter = new TimePeriodAdapter(
					_context,android.R.layout.select_dialog_singlechoice,
					periode_list);
		}	
		else
		{
			throw new Exception("cannot set the adapter of the TimePeriodSpinner...the context is null");
		}
	}	
	
	@Override
	public void setAdapter(SpinnerAdapter adapter) {
		Log.i("TimePeriodSpinner", "setting the adapter...");
		try {
			setList();
		} catch (Exception e) {			
			e.printStackTrace();
		}		
		super.setAdapter(_adapter);			
	}

	@Override
	public SpinnerAdapter getAdapter() {		
		return _adapter;
	}

	public TimePeriodSpinner(Context context) {
		super(context);
		_context = context;		
	}

	public TimePeriodSpinner(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		_context = context;		
	}

	public TimePeriodSpinner(Context context, AttributeSet attrs) {
		super(context, attrs);
		_context = context;		
	}	
}
